package com.colaayi.youhelper;

import com.colaayi.youhelper.light.Lightfragment;
import com.colaayi.youhelper.makemoney.MakeMoneyFragment;
import com.colaayi.youhelper.map.MyMapFragment;
import com.colaayi.youhelper.schedule.ScheduleFragment;


import com.colaayi.youhelper.setting.SettingFragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;


public class MainActivity extends FragmentActivity implements OnCheckedChangeListener ,OnClickListener {
	private FragmentManager fragmentmanager = getSupportFragmentManager();
	private RadioGroup mRadiogroup;
	public static final int FRAGMENT_LIGHT = 1;
	public static final int FRAGMENT_MAKEMONEY = 2; 
	public static final int FRAGMENT_SCHEDULE= 3;
	public static final int FRAGMENT_MAP = 4; 
	public static final int FRAGMENT_SETTINGS =5;
	private int mActionId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initViews();
		initFragment(0);
	}
    private void initViews(){
    	mRadiogroup = (RadioGroup) findViewById(R.id.activity_main_botton_radiogroup);
        mRadiogroup.setOnCheckedChangeListener(this);
        mRadiogroup.setSelected(true);
    }




	@Override
	public void onClick(View arg0) {
	
		
	}
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		initFragment(checkedId);
	}
	/**
	 *fragment
	 */
	 Fragment getFragmentByAction(int actionId) {
		switch (actionId) {
		case FRAGMENT_LIGHT:
			return new Lightfragment();

		case FRAGMENT_MAKEMONEY:
			return new MakeMoneyFragment();

		case FRAGMENT_SCHEDULE:
			return new ScheduleFragment();

		case FRAGMENT_MAP:
			return new MyMapFragment();

		case FRAGMENT_SETTINGS:
			return new SettingFragment();
		}
		return null;

	}
	 /**
	  * 初始化第一次fragment
	  */
	 private void initFragment(int checkedId){
		 Bundle bundle=new Bundle();
         int actionId=FRAGMENT_LIGHT;
         FragmentTransaction ft=fragmentmanager.beginTransaction();
         Fragment fragment=null;
        switch(checkedId){
         case R.id.activity_main_botton_light:
         actionId=FRAGMENT_LIGHT;
         break;
         case R.id.activity_main_botton_makemoney:
        	 actionId=FRAGMENT_MAKEMONEY;
        	 break;
         case R.id.activity_main_botton_schedule:
        	 actionId=FRAGMENT_SCHEDULE;
        	 break;
         case R.id.activity_main_botton_map:
        	 actionId=FRAGMENT_MAP;
        	 break;
         case R.id.activity_main_botton_setting:
        	 actionId=FRAGMENT_SETTINGS;
        	 break;
         }
         mActionId=actionId;
         bundle.putString("actionId", actionId+"");
         fragment = fragmentmanager.findFragmentByTag(actionId + "");
 		if (fragment == null) {
 			fragment = getFragmentByAction(actionId);
 			fragment.setArguments(bundle);
 		}
 		ft.replace(R.id.activity_main_container, fragment, actionId + "");
 		ft.addToBackStack(null);
 		ft.commit();
	 }
}
